package com.songlanyun.modules.donation.controller.admin;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.songlanyun.common.enums.DonationConstant;
import com.songlanyun.modules.donation.model.DonationEntityAuditDTO;
import com.songlanyun.modules.publicbenefitmsg.model.to.PublicBenefitMsgTO;
import com.songlanyun.modules.publicbenefitmsg.service.PublicBenefitMsgService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.donation.entity.DonationEntity;
import com.songlanyun.modules.donation.service.DonationService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;

import javax.annotation.Resource;


/**
 * 公益捐款
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2022-07-21 11:32:51
 */
@RestController
@RequestMapping("admin/donation")
public class DonationController extends AbstractController {
    @Autowired
    private DonationService donationService;

    @Resource
    private PublicBenefitMsgService publicBenefitMsgService;

    /**
     * 列表
     */
    @GetMapping("/page")
    //@RequiresPermissions("donation:donation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = donationService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 公益捐款信息查询
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("donation:donation:info")
    public R info(@PathVariable("id") Long id) {
        DonationEntity donation = donationService.loadById(id, false);
        return R.ok().put("data", donation);
    }

    /**
     * 审核
     */
    @PostMapping("/audit")
    //@RequiresPermissions("donation:donation:save")
    public R audit(@RequestBody DonationEntityAuditDTO dto) {
        dto.setAuditAdmin(this.getUser().getUsername());
        DonationEntity donationEntity = donationService.audit(dto);

        //发送公益消息
        if (donationEntity.getStatus().equals(DonationConstant.DonationStatus.YES)) {
            publicBenefitMsgService.sendMsg(
                    PublicBenefitMsgTO.builder()
                            .type(DonationConstant.MsgType.DONATE)
                            .modelType(DonationConstant.DonateMsgType.PUBLIC_WELFARE_DONATION.getCode())
                            .modelId(donationEntity.getId()).build()
            );
        }
        return R.ok();
    }

}
